//
//  JXAlertview.m
//  基本框架
//
//  Created by runone02 on 2018/7/24.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "JXAlertview.h"


@interface JXAlertview ()<CAAnimationDelegate>
/** */
@property (nonatomic, strong) UIView *view;
/** */
@property (nonatomic) int tag;
@end


@implementation JXAlertview
@synthesize title,message,cancelBtn,sureBtn;

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

-(void)initwithtitle:(NSString *)titleStr andcommitbtn:(NSString *)commit andStr:(NSString *)str {
    self.backgroundColor = [UIColor whiteColor];
    self.userInteractionEnabled = YES;
    self.layer.borderColor = [UIColor clearColor].CGColor;
    self.layer.borderWidth = 1;
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    
    title = [[UILabel alloc]init];
    title.backgroundColor = RGBA(45, 54, 62, 1);
    title.text = titleStr;
    title.textAlignment = NSTextAlignmentCenter;
    title.font = TITLE_FONT(30);
    title.textColor = [UIColor whiteColor];
    [self addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(50);
    }];
    
    sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn addTarget:self action:@selector(selectbtn:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitle:commit forState:0];
    sureBtn.tag = 2;
    [sureBtn setBackgroundColor:RGBA(228, 0, 127, 1)];
    sureBtn.layer.borderWidth = 1;
    sureBtn.layer.borderColor = [UIColor clearColor].CGColor;
    sureBtn.layer.cornerRadius = 5;
    sureBtn.layer.masksToBounds = YES;
    [self addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self).with.offset(-10);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(SCREEN_WIDTH/2 - 50);
    }];
    
    cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [cancelBtn addTarget:self action:@selector(dismmis) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitle:str forState:0];
    cancelBtn.tag = 2;
    [cancelBtn setBackgroundColor:[UIColor redColor]];
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.borderColor = [UIColor clearColor].CGColor;
    cancelBtn.layer.cornerRadius = 5;
    cancelBtn.layer.masksToBounds = YES;
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self).with.offset(-10);
        make.left.mas_equalTo(self).with.offset(20);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(SCREEN_WIDTH/2 - 50);
    }];
    
}

- (void)selectbtn:(id)sender{
    [self dismmis];
    UIButton *btn = (UIButton *)sender;
    [self.delegate btnindex:(int)btn.tag :(int)self.tag];
}

- (void)show{
    UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
    
    self.view = [[UIView alloc]initWithFrame:keyWindow.bounds];
    self.view.alpha = 0.3;
    self.view.backgroundColor = [UIColor blackColor];
    [keyWindow addSubview:self.view];
    self.alpha = 1;
    [keyWindow addSubview:self];
    [keyWindow bringSubviewToFront:self];
    
    CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animation];
    bounceAnimation.duration = 0.3;
    bounceAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    bounceAnimation.values = [NSArray arrayWithObjects:
                              [NSNumber numberWithFloat:0.01],
                              [NSNumber numberWithFloat:1.1],
                              [NSNumber numberWithFloat:0.9],
                              [NSNumber numberWithFloat:1.0],
                              nil];
    
    [self.layer addAnimation:bounceAnimation forKey:@"transform.scale"];
    
    CABasicAnimation *fadeInAnimation = [CABasicAnimation animation];
    fadeInAnimation.duration = 0.3;
    fadeInAnimation.fromValue = [NSNumber numberWithFloat:0];
    fadeInAnimation.toValue = [NSNumber numberWithFloat:1];
    [self.superview.layer addAnimation:fadeInAnimation forKey:@"opacity"];
}

- (void)showView{
    self.tag = 5;
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    self.view = [[UIView alloc] initWithFrame:keyWindow.bounds];
    self.view.alpha = 0.3;
    self.view.backgroundColor = [UIColor blackColor];
    [keyWindow addSubview:self.view];
    
    self.alpha = 1;
    [keyWindow addSubview:self];
    [keyWindow bringSubviewToFront:self];
    
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.3;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = kCATransitionFade;
    animation.subtype = kCATransitionFromBottom;
    [self.layer addAnimation:animation forKey:@"animation"];
    
    CABasicAnimation *fadeInAnimation = [CABasicAnimation animation];
    fadeInAnimation.duration = 0.3;
    fadeInAnimation.fromValue = [NSNumber numberWithFloat:0];
    fadeInAnimation.toValue = [NSNumber numberWithFloat:1];
    [self.superview.layer addAnimation:fadeInAnimation forKey:@"opacity"];
}

//消失
- (void)dismmis {
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^  {
                         self.transform = CGAffineTransformConcat(self.transform, CGAffineTransformMakeScale(0.01, 0.01));
                        self.view.alpha = 0;
                     }
                     completion:^(BOOL finished){
                         [self.view removeFromSuperview];
                         [self removeFromSuperview];
                         
                     }];
}
@end
